package com.gitee.form_token.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gitee.form_token.dto.MenuDto;
import com.gitee.form_token.entity.SysAccount;
import com.gitee.form_token.entity.MyUserDetails;
import com.gitee.form_token.entity.SysRole;
import com.gitee.form_token.mapper.SysAccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.*;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService, UserDetailsManager {

    // 这里使用 @Resource 启动报错
    @Autowired
    private SysAccountMapper accountMapper;

    @Resource
    private SysRoleService roleService;

    @Resource
    private SysResourceService resourceService;

    /**
     * UserDetailsService
     */

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        // 账号信息
        SysAccount myUser = accountMapper.selectByUsername(username);

        if (Objects.isNull(myUser)) {
            throw new UsernameNotFoundException("账号:" + username + "不存在");
        }

        // 角色信息
        String strSql = "SELECT role_id FROM sys_account WHERE account_id =" + myUser.getId();
        List<SysRole> roles = roleService.list(new QueryWrapper<SysRole>().inSql("role_id", strSql));

        // 菜单信息
        List<MenuDto> menuTree = resourceService.menuByRoleId(myUser.getRoleId());
        // JSONArray menuTree = resourceService.menuByRoleId(dbUser.getRoleId());

        // 权限列表
        List<String> codes = accountMapper.selectAuthoritiesByroleId(myUser.getRoleId());

        for (SysRole role : roles) {
            codes.add("ROLE_" + role.getRoleCode());
        }

        UserDetails userDetails = new MyUserDetails(myUser, menuTree, codes);
        System.out.println("MyUserDetailsService -> userDetails==" + userDetails);

        // 默认用户信息 转换 myUser -> UserDetails
        // List<GrantedAuthority> authorities = new ArrayList<>();
        // UserDetails user = User.builder()
        //         .username(myUser.getUsername())
        //         .password(myUser.getPassword())
        //         .authorities(authorities)
        //         .build();

        return userDetails;
    }

    /**
     * UserDetailsManager
     */

    @Override
    public void createUser(UserDetails user) {
        SysAccount myUser = new SysAccount();

        myUser.setUsername(user.getUsername());
        myUser.setPassword(user.getPassword());

        accountMapper.insertUser(myUser);
    }

    @Override
    public void updateUser(UserDetails user) {

    }

    @Override
    public void deleteUser(String username) {

    }

    @Override
    public void changePassword(String oldPassword, String newPassword) {

    }

    @Override
    public boolean userExists(String username) {
        return false;
    }
}

